package com.woniu.web.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.woniu.domain.Category;
import com.woniu.service.CategoryService;
import com.woniu.service.impl.CategoryServiceImpl;

/**
 * Servlet implementation class GetCategoryInfoServlet
 */
@WebServlet("/GetCategoryInfoServlet")
public class GetCategoryInfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetCategoryInfoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		CategoryService cs = new CategoryServiceImpl();
		List<Category> categoryList;
		try {
			categoryList = cs.getCategoryInfo();
			//将list转变成json
			JSONArray array = JSONArray.parseArray(JSON.toJSONString(categoryList));
			//将jsonarray转变成jsontext
			String jsonArrayStr = array.toJSONString();
			System.out.println("jsonArrayStr:\t"+jsonArrayStr);
			response.getWriter().write(jsonArrayStr);
			//把分类信息放入作用域
			//request.setAttribute("categoryList",categoryList);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
